package com.cykj.page04.test;

import java.lang.reflect.Constructor;

/**
 * @BelongsProject: HandTomcat
 * @BelongsPackage: com.cykj.page01.test
 * @Author: WWJ
 * @CreateTime: 2025-06-19 12:23
 * @Description: 反射获取构造方法并创建对象
 * @Version: 1.0
 */
public class Lesson02 {
    public static void main(String[] args) throws Exception {
        //1.获取Student的字节码文件对象
        Class<?> cls = Class.forName("com.cykj.page04.test.Student");
        //2.获取构造方法 public
        Constructor<?>[] constructors = cls.getConstructors();
        //获取构造方法 包括私有的
        Constructor<?>[] declaredConstructors = cls.getDeclaredConstructors();

        //获取指定的构造方法
        Constructor<?> declaredConstructor1 = cls.getDeclaredConstructor(String.class);
        Constructor<?> declaredConstructor2 = cls.getDeclaredConstructor();
        Constructor<?> declaredConstructor3 = cls.getDeclaredConstructor(Integer.class,String.class);
        //暴力破解  私有的构造方法要暴力破解
        declaredConstructor1.setAccessible(true);
        //强制转换为Student类型
        Student o = (Student) declaredConstructor1.newInstance("test");
        System.out.println(o);

    }
}
